<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator=">" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">设备管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">控制箱管理</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content">
      <div class="flex-bet">
        <div class="left-title flex">
          <div class="add-btn" @click="handleAdd()"
            >添加控制箱</div
          >
        </div>
        <el-form
          ref="searchForm"
          :inline="true"
          :model="searchForm"
          class="demo-form-inline"
        >
          <el-form-item label="">
            <el-cascader
              v-model="searchForm.companyId"
              placeholder="请选择公司名称"
              :options="companyArray"
              :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }"
              style="width:150px"
              @change="changeTreeItem"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-select
              v-model="searchForm.baseId"
              placeholder="请选择基地名称"
              @change="onSubmit"
              style="width:150px!important"
              clearable
            >
              <el-option
                v-for="item in baseArray"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-input
              maxlength="50"
              placeholder="请输入控制箱编码"
              v-model="searchForm.code"
              style="width:150px"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-input
              maxlength="50"
              placeholder="请输入控制箱名称"
              v-model="searchForm.name"
              style="width:150px"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-select
              v-model="searchForm.deviceStatus"
              placeholder="请选择设备状态"
              @change="onSubmit"
              style="width:150px!important"
              clearable
            >
              <el-option label="离线" value="0"></el-option>
              <el-option label="在线" value="1"></el-option>
              <el-option label="检修中" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select
              v-model="searchForm.type"
              placeholder="请选择控制箱类型"
              @change="onSubmit"
              style="width:150px!important"
              clearable
            >
              <el-option label="进水控制箱" value="0"></el-option>
              <el-option label="鱼池控制箱" value="1"></el-option>
              <el-option label="尾水控制箱" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" v-if="searchForm.type==2">
            <el-select
              v-model="searchForm.tailWaterModel"
              placeholder="请选择尾水控制箱模式状态"
              @change="onSubmit"
              style="width:150px!important"
              clearable
            >
              <el-option label="排污模式" value="0"></el-option>
              <el-option label="换水模式" value="1"></el-option>
              <el-option label="上清水模式" value="2"></el-option>
              <el-option label="故障" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <div class="search-btn" @click="onSubmit"></div>
          </el-form-item>
          <el-form-item>
            <div class="reset-btn" @click="resetForm"></div>
          </el-form-item>
        </el-form>
      </div>
      <NxTable
        :table-cols="tableCols"
        :table-data="tableData"
        :pag-ination="pagination"
        class="table-wrap"
        @refresh="refresh"
      >
        <template slot="deviceStatus" slot-scope="scope">
          <span v-if="scope.row.deviceStatus == 0">离线</span>
          <span v-if="scope.row.deviceStatus == 1">在线</span>
          <span v-if="scope.row.deviceStatus == 2">检修中</span>
        </template>
        <template slot="type" slot-scope="scope">
          <span v-if="scope.row.type == 0">进水控制箱</span>
          <span v-if="scope.row.type == 1">鱼池控制箱</span>
          <span v-if="scope.row.type == 2">尾水控制箱</span>
        </template>
        <template slot="tailWaterModel" slot-scope="scope">
          <span v-if="scope.row.type == 2&&scope.row.tailWaterModel == 0">排污模式</span>
          <span v-if="scope.row.type == 2&&scope.row.tailWaterModel == 1">换水模式</span>
          <span v-if="scope.row.type == 2&&scope.row.tailWaterModel == 2">上清水模式</span>
          <span v-if="scope.row.type == 2&&scope.row.tailWaterModel == 3">故障</span>
        </template>
        <template slot="operation" slot-scope="scope">
          <span
            @click="handleDetail(scope.row)"
            class="nx-btn nx-edit"
            >详情</span
          >
          <span
            @click="handleEdit(scope.row)"
            class="nx-btn nx-edit"
            >编辑</span
          >
          <span
            @click="handleDelete(scope.row)"
            class="nx-btn nx-del"
            >删除</span
          >
        </template>
      </NxTable>
    </div>
    <AddEditDialog
      :addEditId="addEditId"
      :addEditStatus="addEditStatus"
      :addEditIsDialog="addEditIsDialog"
    ></AddEditDialog>
  </div>
</template>
<script>
import NxTable from "@/components/NxTable.vue";
import AddEditDialog from "./components/addEditDialog.vue";
import { Page } from "@/utils/pagination";
import {
  getObjBoxList,getSysCompanyBases,delObjBox
} from "@/api/system";
import { getCompanyInfoTree } from "@/api/company";

export default {
  components: {
    NxTable,
    AddEditDialog,
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        baseId: "",
        code: "",
        userType: "",
        companyId: Number(window.localStorage.getItem('companyId')),
        deviceStatus: "",
        name: "",
        tailWaterModel: "",
        type: ""
      },
      // 表格字段
      tableCols: [
        {
          label: "公司名称",
          prop: "companyName",
          align: "center",
          width: 250,
        },
        {
          label: "基地名称",
          prop: "baseName",
          align: "center",
          width: 200,
        },
        {
          label: "控制箱编码",
          prop: "code",
          align: "center",
          width: 200,
        },
        {
          label: "控制箱名称",
          prop: "name",
          align: "center",
          width: 200,
        },
        {
          label: "设备状态",
          slot: "deviceStatus",
          align: "center",
          width: 200,
        },
        {
          label: "控制箱类型",
          slot: "type",
          align: "center",
          width: 200,
        },
        {
          label: "尾水控制箱模式状态",
          slot: "tailWaterModel",
          align: "center",
          width: 200,
        },
        {
          label: "操作",
          slot: "operation",
          align: "center",
          width: 180,
          fixed: "right",
        },
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: "", // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: "add",
      addEditId: "", // 添加编辑rowid
      baseArray: [], // 角色集合
      companyArray: [], // 公司集合
      userType: "", // 用户类型（0超级管理员，1公司管理员，2公司员工）
    };
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem("userType"));
    this.searchForm.userType=this.userType
    this.getCompanyInfoTree();
    this.getList();
  },
  // 自定义方法
  methods: {
    handleDetail(row){
      this.$router.push({
        path: `/system/system/sysBoxDetail?id=`+row.id,
      });
    },
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1;
      this.getList();
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum;
      this.searchForm.pageSize = val.pageSize;
      this.getList();
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        baseId: "",
        code: "",
        companyId: "",
        deviceStatus: "",
        name: "",
        tailWaterModel: "",
        type: ""
      };

      this.onSubmit();
    },
    // 查询列表
    getList() {
      getObjBoxList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.data;
          this.pagination.count = res.data.total;
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delObjBox({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.getList();
            }
          });
        })
        .catch((err) => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 添加
    handleAdd() {
      this.addEditId = "";
      this.addEditStatus = "add";
      this.addEditIsDialog = true;
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + "";
      this.addEditStatus = "edit";
      this.addEditIsDialog = true;
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false;
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false;
    },
    // 启用、禁用
    handleStatus(row) {
      openOrCloseSysUser({ id: row.id }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg,
          });
          this.getList();
        }
      });
    },
    // 用户类型切换
    typeChange() {
      this.searchForm.companyId = "";
      this.companyArray = [];

      this.searchForm.roleId = "";
      this.baseArray = [];

      // 获取公司列表
      this.getCompanyInfoTree();

      // 查询
      this.onSubmit();
    },
    // 获取公司列表
    getCompanyInfoTree() {
      if (!this.searchForm.userType) {
        return;
      }

      if (this.searchForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: "总公司",
            parentId: 0,
          },
        ];
      } else {
        getCompanyInfoTree({ id: 0 }).then((res) => {
          if (res.code == 200) {
            this.companyArray = res.data;
          }
        });
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1];
      } else {
        this.searchForm.companyId = "";
      }

      this.searchForm.baseId = "";
      this.baseArray = [];

      this.getList();
      
      // 获取角色列表
      this.getSysCompanyBases();
    },
    // 获取角色列表
    getSysCompanyBases() {
      if (
        this.searchForm.companyId === "" ||
        this.searchForm.companyId == null
      ) {
        return;
      }

      getSysCompanyBases({ id: this.searchForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>